<?php

namespace org;

class Baoguan {

    const SIFN_KEY = 'alexyu01';

    static $url = '';

    static function set_url($url) {
        self::$url = $url;
    }

    //发送订单
    public static function send_order($order_list) {
        return self::send_common('order', $order_list);
    }

    //发送运单
    public static function send_yundan($yundan_list) {
        return self::send_common('yundan', $yundan_list);
    }

    //发送清单
    public static function send_qingdan($qingdan_list) {
        return self::send_common('qingdan', $qingdan_list);
    }

    //发送清单
    public static function send_chedan($datalist) {
        return self::send_common('chedan', $datalist);
    }

    //发送清单
    public static function send_tuihuo($datalist) {
        return self::send_common('tuihuo', $datalist);
    }

    private static function send_common($type, $data) {
        if (self::$url == '') {
            exit('请设置接口地址!');
        }
        $data = json_encode($data);
        $time = time();
        $param = array(
            'type' => $type,
            'data' => $data,
            'timestamp' => $time,
            'sign' => md5($type . $data . $time . self::SIFN_KEY)
        );
        return self::curl_post(self::$url, $param);
    }

    //处理回执
    public static function get_huizhi() {
        $huizhi = isset($_POST['huizhi']) ? $_POST['huizhi'] : '';
        $timestamp = isset($_POST['timestamp']) ? $_POST['timestamp'] : '';
        $sign = isset($_POST['sign']) ? $_POST['sign'] : '';
        if (!empty($huizhi) && !empty($timestamp) && !empty($sign)) {
            $s = md5($huizhi . $timestamp . self::KEY);
            if ($s == $sign) {
                $huizhi = json_decode($huizhi, true);
                if ($huizhi && is_array($huizhi)) {
                    return $huizhi;
                }
            }
        }
        return array();
    }

    private static function curl_post($url = '', $data = array(), $headers = array(), $timeout = 30) {
        $postfield = $s = '';
        if (!empty($data)) {
            if (is_array($data)) {
                foreach ($data as $k => $v) {
                    $postfield .= $s . $k . '=' . rawurlencode($v);
                    $s = '&';
                }
            } else
            if (is_string($data)) {
                $postfield = $data;
            }
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        if ($headers) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfield);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; SeaPort/1.2; Windows NT 5.1; SV1; InfoPath.2)");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        if (stripos($url, 'https://') !== FALSE) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        $value = curl_exec($ch);
        if ($err = curl_error($ch)) {
            
        }
        curl_close($ch);
        return $value;
    }

}
